package 力扣.排序.快速排序;


public class 颜色分类75 {
    public static void main(String[] args) {
        int []nums={2,0,2,1,1,1};
        sortColors(nums);
        //  System.out.println(Arrays.toString(nums));

    }

    /**
     * 三路切分
     * 本题实质就是想让我们将数组分成三部分，即三种颜色的花
     * @param nums
     */
    public static void sortColors(int[] nums) {
        int len = nums.length;
        int l = 0,i = 0,r = len - 1;
        while (i <= r){
            if (nums[i] == 1){
                i++;
            }else if (nums[i] < 1){
                swap(nums,l++,i++);
            }else {
                swap(nums, i, r--);
            }
        }
    }

    private static void swap(int[] nums, int i, int j) {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }

    public static void sortColors2(int[] nums) {
        int l = 0,i = 0,r = nums.length - 1;
        for (int j = 0; j < nums.length; j++) {
            if (nums[i] == 0){
                swap(nums, l++, i++);
            }else if (nums[i] == 1){
                i++;
            }else {
                swap(nums, i, r--);
            }
        }

    }


}
